<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of VirtualMachine
 *
 * @author markos
 */
class VirtualLabs_Rest_VirtualLabs_VirtualMachine {

    protected $_zoneId;
    protected $_logger;
    protected $_debug;

    public function __construct() {
        $this->_logger = Zend_Registry::get(VirtualLabs_Exception::ERROR_LOG);
        $this->_debug = Zend_Registry::get(VirtualLabs_Common::DEBUG_LOG);
    }

    public function listVM($vmId) {
        $config = new Zend_Config_Ini(CLOUDSTACK_CONFIG_FILE, APPLICATION_ENV);
        $this->_zoneId = $config->get("zoneid");
        $this->_logger->debug("[zoneid] = {$this->_zoneId}");
        $url = array();
        $url["response"] = "json";
        $url["command"] = "listVirtualMachines";
        $url["id"] = $vmId;
        $url["zoneid"] = $this->_zoneId;
        $v = new VirtualLabs_Rest_HttpClient_Client();
        try {
            $response = $v->call($url);
        } catch (Zend_Http_Client_Exception $e) {
            $logger = Zend_Registry::get(VirtualLabs_Common::ERROR_LOG);
            $logger->err($e->getMessage());
            $logger->err($e->getTraceAsString());
            throw $e;
        }
        return ($response);
    }

    public function apagarVM($vmId) {
        $config = new Zend_Config_Ini(CLOUDSTACK_CONFIG_FILE, APPLICATION_ENV);
        $this->_zoneId = $config->get("zoneid");
        $this->_logger->debug("[zoneid] = {$this->_zoneId}");
        $url = array();
        $url["response"] = "json";
        $url["command"] = "stopVirtualMachine";
        $url["id"] = $vmId;
        $url["zoneid"] = $this->_zoneId;
        $v = new VirtualLabs_Rest_HttpClient_Client();
        try {
            $response = $v->call($url);
        } catch (Zend_Http_Client_Exception $e) {
            $logger = Zend_Registry::get(VirtualLabs_Common::ERROR_LOG);
            $logger->err($e->getMessage());
            $logger->err($e->getTraceAsString());
            throw $e;
        }
        return ($response);
    }

    public function encenderVM($vmId) {
        $config = new Zend_Config_Ini(CLOUDSTACK_CONFIG_FILE, APPLICATION_ENV);
        $this->_zoneId = $config->get("zoneid");
        $this->_logger->debug("[zoneid] = {$this->_zoneId}");
        $url = array();
        $url["response"] = "json";
        $url["command"] = "startVirtualMachine";
        $url["id"] = $vmId;
        $url["zoneid"] = $this->_zoneId;
        $v = new VirtualLabs_Rest_HttpClient_Client();
        try {
            $response = $v->call($url);
        } catch (Zend_Http_Client_Exception $e) {
            $logger = Zend_Registry::get(VirtualLabs_Common::ERROR_LOG);
            $logger->err($e->getMessage());
            $logger->err($e->getTraceAsString());
            throw $e;
        }
        return ($response);
    }

    //put your code here
    public function createVM($serviceOffering, $diskOffering, $name, $template, $projectId) {
        $config = new Zend_Config_Ini(CLOUDSTACK_CONFIG_FILE, APPLICATION_ENV);
        $this->_zoneId = $config->get("zoneid");
        $this->_logger->debug("[zoneid] = {$this->_zoneId}");
        $url = array();
        $url["response"] = "json";
        $url["command"] = "deployVirtualMachine";
        $url["serviceofferingid"] = $serviceOffering;
        $url["templateid"] = $template;
        $url["diskofferingid"] = $diskOffering;
        $url["displayname"] = $name;
        $url["projectid"] = $projectId;
        $url["name"] = $name;
        $url["zoneid"] = $this->_zoneId;
        $v = new VirtualLabs_Rest_HttpClient_Client();
        try {
            $response = $v->call($url);
        } catch (Zend_Http_Client_Exception $e) {
            $logger = Zend_Registry::get(VirtualLabs_Common::ERROR_LOG);
            $logger->err($e->getMessage());
            $logger->err($e->getTraceAsString());
            throw $e;
        }
        return ($response);
    }
    
    /*
     * 
     */
    public function deleteVM($vmId) {
        $config = new Zend_Config_Ini(CLOUDSTACK_CONFIG_FILE, APPLICATION_ENV);
        $this->_zoneId = $config->get("zoneid");
        $this->_logger->debug("[zoneid] = {$this->_zoneId}");
        $url = array();
        $url["response"] = "json";
        $url["command"] = "destroyVirtualMachine";
        $url["id"] = $vmId;
        $v = new VirtualLabs_Rest_HttpClient_Client();
        try {
            $response = $v->call($url);
            $this->_debug->debug("DeleteVM:" . print_r($response,TRUE));
        } catch (Zend_Http_Client_Exception $e) {
            $logger = Zend_Registry::get(VirtualLabs_Common::ERROR_LOG);
            $logger->err($e->getMessage());
            $logger->err($e->getTraceAsString());
            throw $e;
        }
        return ($response);
    }

}

?>
